package com.itheima.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.itheima.domain.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/user/*")
public class UserServlet extends BaseServlet {
    //查询所有的功能
    private void findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<User> list=userService.findAll();
        //将list转换成json
        String json = obj.writeValueAsString(list);
        //响应json数据
        resp.getWriter().write(json);
    }
    //新增功能
    private void save(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       // System.out.println(".............");
       //1、获取页面数据
        String userStr = req.getParameter("userStr");
        System.out.println(userStr);
        //2、json数据转换成对象
        User user = obj.readValue(userStr, User.class);
       userService.save(user);
    }
    //批量删除
    private void deleteByIds(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取ids
        String ids=req.getParameter("ids");
        //System.out.println(ids);
        userService.deleteByIds(ids);
    }
}
